
#ifndef _SDL_GLAPP_H_
#define _SDL_GLAPP_H_

/* SDL library headers */
#include "SDL.h"
#include "SDL_opengl.h"

#include "common.h"

__BEGIN_NAMESPACE_DECL_

typedef enum {
    USER_EVENT_PROCESS = 1,
    USER_EVENT_REDISPLAY
} SDL_GLApp_Event;

class SDL_GLApp
{
    public:
        /**
         * constructors and destructor
         */
        SDL_GLApp();
        SDL_GLApp(const char *title);
        SDL_GLApp(const char *title, int width, int height);
        virtual ~SDL_GLApp();

        // Initializes the SDL_GLApp by initializing the SDL libraries,
        // creating a drawing context and starting a timer to call the
        // process callback as needed.
        virtual void InitApp();

        // Begins running the main processing loop.
        void Start();

        // sets fullscreen mode
        void setFullscreen (bool fullscreen);
        bool isFullscreen () const;

        // set FSAA (Full screen anti-aliasing)
        void setFSAA (bool fsaa);
        bool isFSAA () const;

        // Number of FSAA samples
        void setSamplesFSAA (int samples);
        int getSamplesFSAA () const;

    protected:
        // Display callback used to render the frame.  
        // Pure virtual display function to be overridden by derived classes.
        virtual void Display();
        // Timer callback for arbitrary processing routines
        virtual void Process();
        // Resize callback
        virtual void Resize(int width, int height);
        // Expose callback
        virtual void Expose();
        // Keyboard callback.
        virtual void Keyboard(const SDLKey& key);
        // Mouse motion callback.
        virtual void MouseMotion(Uint8 state, Uint16 x, Uint16 y);

        // Request that a frame be redisplayed
        void RequestRedisplay();

    private:
        // timer callback to push the USER_EVENT_PROCESS event onto the queue.
        static Uint32 ProcessTimer (Uint32 interval, void *param);
        // Helper function to initialize member variables in constructors
        void init();
        // Initialize the video mode
        void InitVideoMode();

    protected:
        SDL_Surface *screen_; // SDL Drawing surface
        const char *title_;   // Titlebar display
        int width_;           // Width of the screen
        int height_;          // Height of the screen
        int frameCount_;      // Count of frames drawn 
        bool done_;           // Used to determine when the application should 
                              // exit the main loop.
        bool fullscreen_;     // fullscreen or not
        bool fsaa_;           // full screen anti-aliasing
        int fsaaSamples_;     // full screen anti-aliasing samples
        double fps_;          // FPS, computed every two seconds.

    private:
        SDL_TimerID timer;  // Timer used to send a user event to the main loop
};

__END_NAMESPACE_DECL_

#endif
